package com.mmy.controller.hotel;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.hotel.THotelRoom;
import com.mmy.domain.hotel.VHotelBase;
import com.mmy.domain.hotel.VHotelPlan;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.hotel.HotelBaseServiceImpl;
import com.mmy.service.impl.hotel.HotelPlanServiceImpl;
import com.mmy.service.impl.hotel.HotelRoomServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;

@Controller
public class HotelBaseController {
	@Autowired
	HotelBaseServiceImpl hotelBaseServiceImpl;
	@Autowired
	HotelRoomServiceImpl hotelRoomServiceImpl;
	@Autowired
	HotelPlanServiceImpl hotelPlanServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	Common common;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;

	/* 前台 */
	/**
	 * 函数功能说明： 跳转进入前台酒店页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.03.02
	 */

	@RequestMapping(value = "/tofronthotelindex")
	public String toFrontHotelIndex(HttpServletRequest request, Model model, HttpSession session, String pageNumber,
			String pageSize) {
		// 获取当前定位
		String strCityName = (String) session.getAttribute("sessionCity");
		if (strCityName == null) {
			strCityName = "上海市";
		}
		TConstZone dbConstZone = new TConstZone();
		// 搜索条件
		String strHotelName = request.getParameter("strHotelName");
		String strProvinceID = request.getParameter("strProvinceID");
		String strCityID = request.getParameter("strCityID");
		String strHotelPrice1 = request.getParameter("strScenicPrice1");
		String strHotelPrice2 = request.getParameter("strScenicPrice2");
		if (strCityID == null || strCityID == "") {
			// 查询当前定位的ZoneID
			dbConstZone = constZoneServiceImpl.getZoneID(strCityName);
		} else {
			dbConstZone = constZoneServiceImpl.findByZoneID(strCityID);
		}

		// 查询该市的所有景区

		// 查询计划日期大于当前日期的酒店ID 价格区间在搜索范围的
		List<Integer> lstHotelID = hotelPlanServiceImpl.getHotelID(strHotelPrice1, strHotelPrice2);
		List<VHotelBase> lstHotelBase = hotelBaseServiceImpl.findBySearch(pageNumber, pageSize, lstHotelID,
				dbConstZone.getZoneID(), strHotelName);

		for (VHotelBase vHotelBase : lstHotelBase) {
			Integer iZoneID = vHotelBase.getZoneID();
			String province = constZoneServiceImpl.findProvinceStr(iZoneID);
			String city = constZoneServiceImpl.findCityStr(iZoneID);
			vHotelBase.setZoneName(province + city + vHotelBase.getZoneName());
		}
		model.addAttribute("lstHotelBase", lstHotelBase);
		// 当前时间有效的计划最小价格
		List<VHotelPlan> lstHotelPlan = hotelPlanServiceImpl.getMinPrice();
		model.addAttribute("lstHotelPlan", lstHotelPlan);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		HashMap<String, Integer> mapPage = hotelBaseServiceImpl.mapFrontHotelBase(pageNumber, pageSize, lstHotelID);

		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		if (!(strProvinceID == "" || strProvinceID == null)) {
			// 根据省ID查出该省所有的市
			List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(Integer.parseInt(strProvinceID));
			model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		}

		model.addAttribute("strHotelName", strHotelName);
		model.addAttribute("strProvinceID", strProvinceID);
		model.addAttribute("strCityID", strCityID);
		model.addAttribute("strHotelPrice1", strHotelPrice1);
		model.addAttribute("strHotelPrice2", strHotelPrice2);
		return "hotel/HotelIndex";

	}

	/**
	 * 函数功能说明： 跳转 进入前台景区电子票详情信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.24
	 */
	@RequestMapping(value = "/tofronthotelbaseinfo")
	public String toFrontScenicBaseInfo(HttpServletRequest request, Model model) {
//查询该酒店的基本信息
		VHotelBase dbHotelBase = hotelBaseServiceImpl.findByHotelID(request.getParameter("iHotelID"));
		model.addAttribute("dbHotelBase", dbHotelBase);
		// 查询酒店的房间
		List<THotelRoom> lstHotelRoom = hotelRoomServiceImpl.findByHotelID(request.getParameter("iHotelID"));
		model.addAttribute("lstHotelRoom", lstHotelRoom);
		List<VHotelPlan> lstHotelPlan = hotelPlanServiceImpl.getByHotelID(request.getParameter("iHotelID"));
		model.addAttribute("lstHotelPlan", lstHotelPlan);
		// 通过所属区域查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbHotelBase.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbHotelBase.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		// 查询景点所属公司名称
		TSysOrg dbSysOrg = sysOrgServiceImpl.findByOrgID(Integer.parseInt(dbHotelBase.getSpare1()));
		model.addAttribute("strOrgName", dbSysOrg.getOrgName());
		return "hotel/HotelInfo";
	}
	/**
	 * 函数功能说明：获得全部酒店信息
	 *
	 * @param 参数说明 tHotelBase
	 * @return 返回值说明 jsp页面
	 * 
	 * @author 创建人 hanxl 修改人hanxl
	 * @date 创建日期 2020.07.28 修改时间2021.03.17
	 */

	@RequestMapping(value = "/backtohotelindex")
	public String backToHotelIndex(Model model) {

		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		return "webbackground/hotelmanagement/HotelIndex";
	}
	
	/**
	  * @descr 级联选择器,本地酒店
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-21
	 */
	@RequestMapping("/getHotelsByZone")
	@ResponseBody
	public String getScenicsByOrg(HttpServletRequest request,HttpSession session) {
		
		//TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		//int iOrgID = tSysUser.getOrgID();
		int iZoneID = Integer.parseInt(request.getParameter("iZoneID"));
		String res = hotelBaseServiceImpl.hotelSelect(iZoneID);
		
		return res;
	}
}
